#!/usr/bin/perl
use strict;
use modules::encode;
package csedit;

sub csedit {
    my ($dbh,$remote,$input,$csnum) = @_;
    my $sth;my $auth;
    if ($$input[6] eq "") {
	print $remote "$csnum O $$input[0] :Syntax :EDIT <#channel> <rnick> <level>\n";
    }
    elsif ($$input[6] !~ /\d+/ || $$input[6] < 50 || $$input[6] > 499) {
	print $remote "$csnum O $$input[0] :Invalid level. use range 50 ~ 499\n";
    }
    else {
	$sth=$dbh->prepare( "SELECT auth FROM ninfo WHERE num = '$$input[0]' AND code = '".encode::encode($$input[0])."';" );
	$sth->execute;
	$auth = $sth->fetchrow_array;
	if ($auth eq '0') {
	    print $remote "$csnum O $$input[0] :Identification with nickserv is required before using this command.\n";
	}
	else {
	    $sth=$dbh->prepare("SELECT level FROM users where rnick = '$auth' AND channel = '$$input[4]';");
	    $sth->execute;
	    my $lv = $sth->fetchrow_array;
	    $sth=$dbh->prepare("SELECT level FROM users WHERE rnick = '$$input[5]' AND channel = '$$input[4]';");
	    $sth->execute;
	    my $lv2 = $sth->fetchrow_array;
	    if ($lv < 450) {
		print $remote "$csnum O $$input[0] :Access denied.\n";
	    }
	    elsif ($lv <= $lv2) {
		print $remote "$csnum O $$input[0] :Try harder next time.\n";
	    }
	    elsif ($$input[6] >= $lv) {
		print $remote "$csnum O $$input[0] :How come he gets a higher access than you?\n";
	    }
	    else {
		$sth=$dbh->prepare("UPDATE users SET level = '$$input[6]' WHERE channel = '$$input[4]' AND rnick = '$$input[5]';");
		$sth->execute;
		print $remote "$csnum O $$input[0] :Changed \cB$$input[5]\cB's level in $$input[4] to $$input[5]\n";
	    }
	}
    }

 
}
1;
